package com.robot.member.controller;

import com.alibaba.fastjson.JSON;
import com.robot.member.pojo.Member;
import com.robot.member.pojo.vo.Authorization;
import com.robot.member.pojo.vo.AuthorizationAccessToken;
import com.robot.member.pojo.vo.WeChatInfo;
import com.robot.member.service.MemberService;
import com.robot.member.service.WeChatService;
import entity.Result;
import entity.StatusCode;
import entity.weixin.template.PaySuccess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import util.JwtUtil;

/**
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/member")
public class WeChatController {
    @Value("${wechat.appid}")
    private String appId;

    @Value("${wechat.secret}")
    private String secret;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private WeChatService weChatService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 获取一般支持 access_token
     *
     * @return
     */
    @RequestMapping(value = "/getNormalAccessToken", method = RequestMethod.GET)
    public Result getNormalAccessToken() {
        String normalAccessToken = weChatService.getClientCredentialAccessToken();
        return new Result(StatusCode.SUCCESS_QUERY, normalAccessToken);
    }

    /**
     * 获取openId
     *
     * @param code
     * @return
     */
    @RequestMapping(value = "/openid/{code}", method = RequestMethod.GET)
    public Result getWeChatOpenId(@PathVariable String code) {
        logger.info("code=" + code);
        //通过code获取授权accessToken和openId
        AuthorizationAccessToken accessToken = weChatService.getAuthorizationAccessToken(code);
        logger.info("accessToken=" + accessToken.toString());
        //通过accessToken和openId 获取微信用户信息
        WeChatInfo weChatInfo = weChatService.getUserInfoByAuthorizationAccessTokenAndOpenId(accessToken.getAccessToken(), accessToken.getOpenId());
        //检查这个微信号是否已经是会员，如果是直接登录，如果不是则注册之后再登录
        Member member = memberService.findByOpenId(weChatInfo.getOpenId());
        if (member == null) {
            //注册
            member = memberService.addByWeChat(weChatInfo);
        }
        logger.info("开始组装返回信息");
        //登录
        String role = "member";
        String memberId = member.getId();
        String token = jwtUtil.createJwt(memberId, member.getOpenId(), role);
        Authorization authorization = new Authorization(memberId, weChatInfo.getOpenId(), role, token);
        logger.info(authorization.toString());
        return new Result(StatusCode.SUCCESS, authorization);
    }

    @RequestMapping(value = "test",method = RequestMethod.GET)
    public Result test(){

        PaySuccess paySuccess=new PaySuccess();
        paySuccess.setRecipient("o6H22s_msJ5iJRpe0sy4VyZucB5Y");
        paySuccess.setTemplateId("nNwPM0kmNw8jxLp_P5gKlvq7JhDIdYrVkpNLGAprG8w");
        paySuccess.setFirst("我是一个标题");
        paySuccess.setKeyword1("示例会员卡号");
        paySuccess.setKeyword2("示例会员姓名");
        paySuccess.setKeyword3("示例消费金额");
        paySuccess.setKeyword4("示例卡内扣除");
        paySuccess.setKeyword5("账户余额");
        paySuccess.setRemark("示例备注");


        rabbitTemplate.convertAndSend("notify", paySuccess.getClass()+"-notify-"+JSON.toJSONString(paySuccess) );
        return null;
    }
}
